/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.formaters.om;

/**
 *
 * @author Mr.Sam
 */
import fr.ove.utils.*;
import fr.ove.openmath.jome.model.*;
import fr.ove.openmath.jome.formaters.om.*;

/**
* Formats the formula tree structure as its corresponding OpenMath object.<BR>
* This is the formater for integrals.
*
* @author © 2000 DIRAT Laurent
* @version 1.0 21/02/2000
*/
public class IntegralFormater implements Formater {
    /**
    * Returns the specified object formatted as a string.
    * @param formatedObject the formatted object (for structured object, could represents the beginning).
    * @param formaterRepository where the different other formaters are.
    * @param obj the object to format.
    */
    public String format(String formatedObject, FormaterRepository formaterRepository, Object obj) {
        OpenMathFormater omFormater = (OpenMathFormater) formaterRepository;
        FormulaTreeStructure fts = (FormulaTreeStructure) obj;
        
        FormulaTreeStructure ftsChild;
        Formater formater;
        
        String objectId = fts.getResourceIdentifier();
        
        int nbChildren = fts.getNbChildren();
        
        // Si ne nbre de fils est 2 ou 4, alors on a une intégrale "OM-exportable"
        // Si 2, alors intégrale indéfinie, sinon, intégrale définie
        if ((nbChildren == 2) || (nbChildren == 4)) {
            String symbName = (nbChildren == 2) ? "int" : "defint";
            formatedObject += omFormater.writeStartApplication();
            // On récupère le nom du CD, puisque le symbole est déterminé ici.
            formatedObject += omFormater.writeSymbol(omFormater.getCdName(objectId), symbName);
            
            // Faut spécifier l'intervalle
            if (nbChildren == 4) {
                formatedObject += omFormater.writeStartApplication();
                formatedObject += omFormater.writeSymbol("interval", "interval");
                
                ftsChild = (FormulaTreeStructure) fts.getChild(2).getChild(0);
                objectId = ftsChild.getResourceIdentifier();
                formater = formaterRepository.getFormater(objectId);
                formatedObject = formater.format(formatedObject, formaterRepository, ftsChild);
                
                ftsChild = (FormulaTreeStructure) fts.getChild(3);
                objectId = ftsChild.getResourceIdentifier();
                formater = formaterRepository.getFormater(objectId);
                formatedObject = formater.format(formatedObject, formaterRepository, ftsChild);
                
                formatedObject += omFormater.writeEndApplication();
            }
            
            // La fonction intégrée
            // Création du bind
            formatedObject += omFormater.writeStartBinding();
            formatedObject += omFormater.writeSymbol("lambda");
            // On met la variable liée.
            formatedObject += omFormater.writeStartBoundVariables();
            
            ftsChild = (FormulaTreeStructure) fts.getChild(1);
            objectId = ftsChild.getResourceIdentifier();
            formater = formaterRepository.getFormater(objectId);
            formatedObject = formater.format(formatedObject, formaterRepository, ftsChild);
            
            formatedObject += omFormater.writeEndBoundVariables();
            
            // On met maintenant l'objet du bind, i.e. l'argument.
            ftsChild = (FormulaTreeStructure) fts.getChild(0);
            objectId = ftsChild.getResourceIdentifier();
            formater = formaterRepository.getFormater(objectId);
            formatedObject = formater.format(formatedObject, formaterRepository, ftsChild);
            
            formatedObject += omFormater.writeEndBinding();
            formatedObject += omFormater.writeEndApplication();
        }
        
        return formatedObject;
    }
}